module OH_RPOH(
   GLB_RESET,
  // Input data bus from frame
   DBIN_RCLK_155M,
   DBIN_DATA,
   DBIN_J1,
   DBIN_SPE,
   DBIN_CNT8,
  // MPI interface signals
   MPI_FORCE_RAIS,
   MPI_RC2,
   MPI_RG1,
   MPI_RJ1_MODE,
   MPI_RJ1_DATA,
   MPI_RB3COUNT,
   MPI_RB3PULSE,
   MPI_REICOUNT,
   MPI_REIPULSE
   );

input                        GLB_RESET;

  // Input data bus from frame
input                        DBIN_RCLK_155M;
input[63:0]                  DBIN_DATA;
input                        DBIN_J1;
input                        DBIN_SPE;
input[2:0]                   DBIN_CNT8;
  // MPI interface signals
input                        MPI_FORCE_RAIS;
input                        MPI_RJ1_MODE;
output reg[7:0]              MPI_RC2;
output reg[7:0]              MPI_RG1;
output reg[511:0]            MPI_RJ1_DATA;
output reg[3:0]              MPI_RB3COUNT;
output reg                   MPI_RB3PULSE;
output reg[3:0]              MPI_REICOUNT;
output reg                   MPI_REIPULSE;

reg[8:0]                     DBIN_CNT261;
reg[3:0]                     DBIN_CNT9;

reg                          CFG_FORCE_RAIS;
reg                          CFG_RJ1_MODE;

reg[15:0]                    RJ1_0D0A_SREG;
reg[5:0]                     RJ1_SQ_CNT;

reg[7:0]                     RB3_CALC;
reg[7:0]                     RB3_SUM_RESULT;
wire[7:0]                    RB3_CHECK;



//****** Section 1: J0 byte ******//
// cpature CPU configuration CFG_FORCE_RAIS, CFG_RJ1_MODE with data clock
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 ) begin
      CFG_FORCE_RAIS                      <= 1'b0;
      CFG_RJ1_MODE                        <= 1'b0;
   end
   else begin
      CFG_FORCE_RAIS                      <= MPI_FORCE_RAIS;
      CFG_RJ1_MODE                        <= MPI_RJ1_MODE;
   end
end

always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      DBIN_CNT261[8:0]                    <= 9'd0;
   else if( DBIN_CNT8[2:0]==3'b111 )begin
      if ( DBIN_J1==1'b1 && DBIN_SPE==1'b1 )
         DBIN_CNT261[8:0]                 <= 9'd1;
      else if ( DBIN_SPE==1'b1 && DBIN_CNT261[8:0]==9'd260 )
         DBIN_CNT261[8:0]                 <= 9'd0;
      else if ( DBIN_SPE==1'b1 )
         DBIN_CNT261[8:0]                 <= DBIN_CNT261[8:0] +9'd1;
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      DBIN_CNT9[3:0]                      <= 4'd0;
   else if ( DBIN_CNT8[2:0]==3'b111 ) begin
      if ( DBIN_J1==1'b1 && DBIN_SPE==1'b1 )
         DBIN_CNT9[3:0]                   <= 4'd0;
      else if ( DBIN_SPE==1'b1 && DBIN_CNT261[8:0]==9'd260 && DBIN_CNT9[3:0]==4'd8)
         DBIN_CNT9[3:0]                   <= 4'd0;
      else if ( DBIN_SPE==1'b1 && DBIN_CNT261[8:0]==9'd260)
         DBIN_CNT9[3:0]                   <= DBIN_CNT9[3:0] +4'd1;
   end
end

// J1 count generate
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      RJ1_0D0A_SREG[15:0]                 <= 16'd0;
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd0 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000) begin
         RJ1_0D0A_SREG[15:8]              <= RJ1_0D0A_SREG[7:0];
         RJ1_0D0A_SREG[7:0]               <= DBIN_DATA[63:56];
      end
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      RJ1_SQ_CNT[5:0]                     <= 6'd0;
   else begin
      if ( CFG_RJ1_MODE==1'b1 ) begin
         if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd0 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 ) begin
            if ( RJ1_0D0A_SREG[15:0]==16'h0d0a )
               RJ1_SQ_CNT[5:0]            <= 6'd1;
            else
               RJ1_SQ_CNT[5:0]            <= RJ1_SQ_CNT[5:0] +6'd1;
         end
      end
      else begin
         if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd0 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 ) begin
            if ( DBIN_DATA[63]==1'b1 )
               RJ1_SQ_CNT[5:0]            <= 6'd1;
            else
               RJ1_SQ_CNT[5:0]            <= RJ1_SQ_CNT[5:0] +6'd1;
         end
      end
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_RJ1_DATA[511:0]                   <= 512'd0;
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd0 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 ) begin
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8]    <= DBIN_DATA[56];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+1]  <= DBIN_DATA[57];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+2]  <= DBIN_DATA[58];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+3]  <= DBIN_DATA[59];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+4]  <= DBIN_DATA[60];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+5]  <= DBIN_DATA[61];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+6]  <= DBIN_DATA[62];
         MPI_RJ1_DATA[RJ1_SQ_CNT[5:0]*8+7]  <= DBIN_DATA[63];
      end
   end
end

always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_RC2[7:0]                    <= 8'd0;
   else if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd2 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 ) begin
      MPI_RC2[7:0]                    <= DBIN_DATA[63:56];
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_RG1[7:0]                    <= 8'd0;
   else if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd3 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 ) begin
      MPI_RG1[7:0]                    <= DBIN_DATA[63:56];
   end
end



always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 ) begin
      RB3_CALC[7:0]                  <= 8'd0;
   end
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd0 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 )
         RB3_CALC[7:0]               <= DBIN_DATA[7:0] ^ DBIN_DATA[15:8] ^ DBIN_DATA[23:16] ^ DBIN_DATA[31:24] ^ DBIN_DATA[39:32] ^ DBIN_DATA[47:40] ^ DBIN_DATA[55:48] ^ DBIN_DATA[63:56];
      else if ( DBIN_SPE==1'b1 )
         RB3_CALC[7:0]               <= RB3_CALC[7:0] ^ DBIN_DATA[7:0] ^ DBIN_DATA[15:8] ^ DBIN_DATA[23:16] ^ DBIN_DATA[31:24] ^ DBIN_DATA[39:32] ^ DBIN_DATA[47:40] ^ DBIN_DATA[55:48] ^ DBIN_DATA[63:56];
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      RB3_SUM_RESULT[7:0]            <= 8'd0;
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd8 && DBIN_CNT261[8:0]==9'd260 && DBIN_CNT8[2:0]==3'b111 )
         RB3_SUM_RESULT[7:0]         <= RB3_CALC[7:0] ^ DBIN_DATA[7:0] ^ DBIN_DATA[15:8] ^ DBIN_DATA[23:16] ^ DBIN_DATA[31:24] ^ DBIN_DATA[39:32] ^ DBIN_DATA[47:40] ^ DBIN_DATA[55:48] ^ DBIN_DATA[63:56];
   end
end
  assign RB3_CHECK[7:0]=RB3_SUM_RESULT[7:0] ^ DBIN_DATA[63:56];

always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_RB3COUNT[3:0]                 <= 4'd0;
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd1 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 )
         MPI_RB3COUNT[3:0]              <= {3'd0, RB3_CHECK[7]} + {3'd0, RB3_CHECK[6]} + {3'd0, RB3_CHECK[5]} + {3'd0, RB3_CHECK[4]} + {3'd0, RB3_CHECK[3]} + {3'd0, RB3_CHECK[2]} + {3'd0, RB3_CHECK[1]} +{3'd0, RB3_CHECK[0]};
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_RB3PULSE                      <= 1'b0;
   else begin
      MPI_RB3PULSE                      <= (DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd1 && DBIN_CNT261[8:0]==9'd0);
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_REICOUNT                      <= 4'd0;
   else begin
      if ( DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd3 && DBIN_CNT261[8:0]==9'd0 && DBIN_CNT8[2:0]==3'b000 )
         MPI_REICOUNT                   <= DBIN_DATA[63:60];
   end
end
always @( posedge DBIN_RCLK_155M or posedge GLB_RESET ) begin
   if ( GLB_RESET==1'b1 )
      MPI_REIPULSE                      <= 1'b0;
   else
      MPI_REIPULSE                      <= (DBIN_SPE==1'b1 && DBIN_CNT9[3:0]==4'd3 && DBIN_CNT261[8:0]==9'd0);
end

endmodule